﻿import request from '/@/utils/request';
enum Api {
  AddProject = '/api/project/add',
  DeleteProject = '/api/project/delete',
  BatchDeleteProject = '/api/project/batchDelete',
  deleteProjectMapDetail = '/api/project/deleteProjectMapDetail',
  UpdateProject = '/api/project/update',
  ImportProject = '/api/project/import',
  DownLoadProject = '/api/Project/DownLoad',
  DetailProject = '/api/project/detail',
  PageProject = '/api/project/page',
  getPageProject = '/api/project/GetProjectMapDetail',
  UploadCADFiles = '/api/project/UploadCADFiles',
  SumDateMap = '/api/Project/sumdateMap/{projectId}/{projectMapDetailId}',
  DownLoadPdf = '/api/Project/download/{projectId}/{projectMapDetailId}'


}


/**
 * 获取项目详细信息的汇总数据
 * @param projectId 项目 ID (integer 类型)
 * @param projectMapDetailId 项目地图详情 ID (integer 类型)
 */
export const sumDateMap = (projectId: any, projectMapDetailId: any) => {
	// 将参数值替换到 URL 模板中
	const url = Api.SumDateMap
		.replace('{projectId}', encodeURIComponent(projectId.toString()))
		.replace('{projectMapDetailId}', encodeURIComponent(projectMapDetailId.toString()));

	return request({
		url,
		method: 'get',
	});
};

/**
 * 下载项目文件
 * @param projectId 项目 ID
 * @param projectMapDetailId 项目地图详情 ID
 */
export const downloadProjectFile = (projectId: any, projectMapDetailId: string) => {
	// 构造请求 URL
	const url = Api.DownLoadPdf
		.replace('{projectId}', encodeURIComponent(projectId))
		.replace('{projectMapDetailId}', encodeURIComponent(projectMapDetailId));

	// 发起请求，设置 responseType 为 'blob' 以处理文件下载
	return request({
		url,
		method: 'get',
		responseType: 'blob' // 设置响应类型为 blob，用于处理文件下载
	});
};




// 导入
export const importProject = (params?: any) => {
	const formData = new FormData();
	formData.append('file', params);
	// 请求配置
	const config = {
		headers: {
		},
	};
	request({
		url: Api.ImportProject,
		method: 'post',
		headers:{'Content-Type' : 'multipart/form-data'},
		data: formData,
		// data:formData
	});
}
// 增加项目
export const addProject = (params?: any) =>
	request({
		url: Api.AddProject,
		method: 'post',
		data: params,
	});

// 删除项目
export const deleteProject = (params?: any) =>
	request({
			url: Api.DeleteProject,
			method: 'post',
			data: params,
		});

		// 删除项目方案
export const deleteProjectMapDetail = (params?: any) =>
	request({
			url: Api.deleteProjectMapDetail,
			method: 'post',
			data: params,
		});
// 批量删除项目
export const batchDeleteProject = (params?: any) =>
	request({
			url: Api.BatchDeleteProject,
			method: 'post',
			data: params,
		});

// 编辑项目
export const updateProject = (params?: any) =>
	request({
			url: Api.UpdateProject,
			method: 'post',
			data: params,
		});
export const detailProject = (params?: any) =>
	request({
		url: Api.DetailProject,
		method: 'get',
		params: params
	});

// 分页查询项目
export const pageProject = (params?: any) =>
	request({
			url: Api.PageProject,
			method: 'post',
			data: params,
		});

//下载项目模板
export const downLoadProject = (params?: any) =>
	request({
			url: Api.DownLoadProject,
			method: 'post',
			data: params,
			responseType: 'blob',
		});
	/**
	* 上传CAD
	*/
export const uploadCADFiles = (params: any) =>
		uploadFileHandle(params, Api.UploadCADFiles)

	export const uploadFileHandle = (params: any, url: string) => {
	const formData = new window.FormData();
	formData.append('file', params.file);
	//自定义参数
	if (params.data) {
		Object.keys(params.data).forEach((key) => {
			const value = params.data![key];
			if (Array.isArray(value)) {
				value.forEach((item) => {
					formData.append(`${key}[]`, item);
				});
				return;
			}
			formData.append(key, params.data![key]);
		});
	}
	return request({
		url: url,
		method: 'POST',
		data: formData,
		headers: {
			'Content-type': 'multipart/form-data;charset=UTF-8',
			// ts-ignore
			ignoreCancelToken: true,
		},
	});
};
